package cashpointer.mobile;

import java.util.List;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.LinearLayout;
import android.widget.ZoomControls;
import cashpointer.mobile.support.ItemizedOverlayManager;
import cashpointer.mobile.support.PreferencesManager;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

public class CashpointMapActivity extends MapActivity {
	/**
	 * Opcje menu.
	 */
	private static final int MENU_QUIT = Menu.FIRST;
	
	//private static final String LOG_TAG = "CashpointMapActivity";
	
	// Zmienne potrzebne do wyswietlenia mapy
	private LinearLayout linearLayout = null;
	private MapView mapView = null;
	private ZoomControls mZoom = null;
	private List<Overlay> mapOverlays = null;
	private Drawable drawable = null;
	private ItemizedOverlayManager itemizedOverlay = null;
	
	@Override
	protected boolean isRouteDisplayed() {
		return false;
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
        MapController mc = mapView.getController(); 
        switch (keyCode) {
            case KeyEvent.KEYCODE_3:
                mc.zoomIn();
                break;
            case KeyEvent.KEYCODE_1:
                mc.zoomOut();
                break;
        }
        return super.onKeyDown(keyCode, event);
    }   
	
    @SuppressWarnings("deprecation")
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.cashpoint_map);

        Intent intent = getIntent();
        
        // Pobieram polozenie geograficzne bankomatu wybranego z listy
        // latitude
        double[] latitudeArray = intent.getDoubleArrayExtra(PreferencesManager.CASHPOINT_SELECTED_LATITUDE);
        
        // longitude
        double[] longitudeArray = intent.getDoubleArrayExtra(PreferencesManager.CASHPOINT_SELECTED_LONGITUDE);
                
        // cityStreet
        String[] cityStreetArray = intent.getStringArrayExtra(PreferencesManager.CASHPOINT_SELECTED_CITY_STREET);
        
        // owner
        String[] ownerArray = intent.getStringArrayExtra(PreferencesManager.CASHPOINT_SELECTED_OWNER);
        
        if ((longitudeArray.length == cityStreetArray.length) && (cityStreetArray.length == ownerArray.length) && (latitudeArray.length == longitudeArray.length)) {
            // Ustawienie widoku mapy
            linearLayout = (LinearLayout) findViewById(R.id.zoomview);
            mapView = (MapView) findViewById(R.id.mapview);
            mZoom = (ZoomControls) mapView.getZoomControls();
            linearLayout.addView(mZoom);
            mapView.setStreetView(true);
            
            mapOverlays = mapView.getOverlays();
            drawable = this.getResources().getDrawable(R.drawable.bubble_icon);
            itemizedOverlay = new ItemizedOverlayManager(drawable);
            itemizedOverlay.setContext(getApplicationContext());
            
            double middleLatitude = 0.0;
            double middleLongitude = 0.0;
            int middleCount = 0;
            
            // Ustawienie punktu na mapie
            for (int i = 0; i < longitudeArray.length; i++) {
            	double latitude = latitudeArray[i];
            	double longitude = longitudeArray[i];
            	String cityStreet = cityStreetArray[i];
            	String owner = ownerArray[i];
            	
            	if (latitude != (double)0 && longitude != (double)0 && cityStreet != null && owner != null) {
            		// Potrzebne do wyznaczenia srodka
            		middleLatitude += latitude;
            		middleLongitude += longitude;
            		middleCount++;
            		
            		// Dodanie pozycji
		            int finalLatitude = (int)(latitude * 1e6);
		            int finalLongitude = (int)(longitude * 1e6);
		            GeoPoint point = new GeoPoint(finalLatitude, finalLongitude);
		            OverlayItem overlayitem = new OverlayItem(point, cityStreet, owner);
		            itemizedOverlay.addOverlay(overlayitem);
		            mapOverlays.add(itemizedOverlay);
            	}
            }
            
            // Usredniam
            middleLatitude /= middleCount;
            middleLongitude /= middleCount;
            
            // Wycentrowanie mapy na wyswietlanym punkcie
        	MapController mapController = mapView.getController();
            GeoPoint geoPoint = new GeoPoint((int)(middleLatitude * 1e6), (int)(middleLongitude * 1e6));
            mapController.animateTo(geoPoint);
            mapController.setZoom(15); 
            mapView.invalidate();
            	
        } else {
        	//Log.e(LOG_TAG, "Tablice sa roznych rozmiarow!");
        	quit();
        }
    }
    
    /**
     * Funkcja budowania menu.
     */
    public boolean onCreateOptionsMenu(Menu menu) {
        menu.add(0, MENU_QUIT, 0, getString(R.string.menu_exit)).setIcon(R.drawable.menu_close);
        return true;
    }

    /**
     * Obsluga zdarzenia wybrania opcji z menu.
     */
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        	case MENU_QUIT: {
        		quit();
        		return true;
        	}
        }
	    
        return false;
    }

    /**
     * Funkcja konczenia aplikacji.
     */
    private void quit() {
    	this.finish();
    }
}